//
//  NewsViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/17.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "NewsViewController.h"
#import "News.h"
#import "common.h"
#import "NewCell.h"
#import "DataService.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"

@interface NewsViewController () <UITableViewDataSource, UITableViewDelegate> {
    UITableView *_table;
    NSMutableArray *_news;
    UIImageView *_image;
    UILabel *_label;
}

@end

@implementation NewsViewController

#pragma mark - viewController's function

- (nonnull instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = @"新闻";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self createTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView's delegate

- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (((News *)_news[indexPath.row]).type == 1) {
        DetailViewController *detail = [[DetailViewController alloc] init];
        [self.navigationController pushViewController:detail animated:YES];
       
    }
}



- (void)createTableView  {
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49)];
    _table.delegate = self;
    _table.dataSource = self;
    UINib *nib = [UINib nibWithNibName:newCell bundle:[NSBundle mainBundle]];
    [_table registerNib:nib forCellReuseIdentifier:newCell];
    
    News *new = _news[0];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    _table.tableHeaderView = view;
    _image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kWidth, 200)];
    [_image sd_setImageWithURL:[NSURL URLWithString:new.image] placeholderImage:[UIImage imageNamed:@"4"]];
    _label = [[UILabel alloc] initWithFrame:CGRectMake(5, 244, 300, 20)];
    _label.text = new.title;
    _label.textColor = [UIColor whiteColor];
    [_news removeObjectAtIndex:0];
    [self.view addSubview:_table];
    [self.view addSubview:_image];
    [self.view addSubview:_label];
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 100;
}



- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NewCell *cell = [tableView dequeueReusableCellWithIdentifier:newCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    cell.news = _news[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
//下拉放大
- (void)scrollViewDidScroll:(nonnull UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    if (y > -64) {
        _image.top = -y;
    } else {
        CGFloat height = 200 - 64 - y;
        CGFloat scale = height / 200;
        _image.transform = CGAffineTransformMakeScale(scale, scale);
        _image.top = 64;
    }
    CGRect frame = _label.frame;
    frame.origin.y = CGRectGetMaxY(_image.frame) - 20;
    _label.frame = frame;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_news count];
}

#pragma mark - loadData

- (void)loadData {
    _news = [[NSMutableArray alloc] init];
    NSArray *array = [DataService readJsonFlieWithNamed:newJson];
    for (int i = 0; i < [array count]; i++) {
        NSDictionary *dic = array[i];
        News *new = [[News alloc] init];
        new.image = [dic objectForKey:@"image"];
        new.title = [dic objectForKey:@"title"];
        new.summary = [dic objectForKey:@"summary"];
        new.type = [[dic objectForKey:@"type"] integerValue];
        [_news addObject:new];
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
